﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MyGeneration.dOOdads;
using MethodIT.Helper;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;

namespace Appraisal.Web.ZB
{
    public partial class ChooseUmp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int nSubID = HelperURL.GetIntFromUrlString("SubID");
                int nKpID = HelperURL.GetIntFromUrlString("KpID");
                int TypeID = HelperURL.GetIntFromUrlString("Type");

                vw_Subject_Table vw = new vw_Subject_Table();
                vw.Where.SubjectID.Value = nSubID;
                vw.Where.KpID.Value = nKpID;
                vw.Query.Load();
                labSubjectName.Text = vw.SubjectName;
                labKpName.Text = vw.KpName;

                LoadData(nSubID, nKpID, TypeID);
            }
        }

        private void LoadData(int SubID,int KpID,int TypeID)
        {

            string UmpSelected = "0";
            vw_SubKpUmp vw = new vw_SubKpUmp();
            vw.Where.SubID.Value = SubID;
            vw.Where.KpID.Value = KpID;
            vw.Where.UmpType.Value = TypeID;
            vw.Query.AddOrderBy(vw_SubKpUmp.ColumnNames.UmpName, WhereParameter.Dir.ASC);
            if (vw.Query.Load())
            {
                do 
                {
                    UmpSelected += vw.UmpID + ",";
                } 
                while (vw.MoveNext());
                UmpSelected += "0";
            }
            //排除已选择的人员
            ZB_Ump ump = new ZB_Ump();
            ump.Where.Enable.Value = "Y";
            ump.Where.ID.Value = UmpSelected;
            ump.Where.ID.Operator = WhereParameter.Operand.NotIn;
            if (txtName.Text != "")
            {
                ump.Where.Name.Value = "%" + txtName.Text + "%";
                ump.Where.Name.Operator = WhereParameter.Operand.Like;
            }
            ump.Query.Load();
            dg.DataSource = ump.DefaultView;
            dg.DataBind();


        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            doSave();
        }

        private void doSave()
        {
            KP_Subject_Table_Ump kpUmp = new KP_Subject_Table_Ump();
            int SubID = HelperURL.GetIntFromUrlString("SubID");
            int KpID = HelperURL.GetIntFromUrlString("KpID");
            int TypeID = HelperURL.GetIntFromUrlString("Type");

            foreach (DataGridItem dgi in dg.Items)
            {
                if (dgi.ItemType == ListItemType.Item || dgi.ItemType == ListItemType.AlternatingItem)
                {
                    CheckBox chkSelectPending = (CheckBox)dgi.FindControl("chkSelectPending");
                    if (chkSelectPending.Checked)
                    {
                        int UmpID = Int32.Parse(dgi.Cells[1].Text);
                        kpUmp.AddNew();
                        kpUmp.SubjectID = SubID ;
                        kpUmp.KpTableID = KpID ;
                        kpUmp.UmpID = UmpID ;
                        kpUmp.UmpType = TypeID ;
                        kpUmp.Status = 0 ;
                    }
                }
            }
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();
                kpUmp.Save();
                tx.CommitTransaction();
                GoBack();
            }
            catch
            {
                tx.RollbackTransaction();
                TransactionMgr.ThreadTransactionMgrReset();
            }
        }

        private void GoBack()
        {
            int SubID = HelperURL.GetIntFromUrlString("SubID");
            int KpID = HelperURL.GetIntFromUrlString("KpID");
            string URL = string.Format("SubjectKpUmpList.aspx?SubID={0}&KpID={1}", SubID, KpID);
            HelperURL.Redirect(URL);
        }

        protected void lbtnBack_Click(object sender, EventArgs e)
        {
            GoBack();
        }

        protected void lbtnMakeChoose_Click(object sender, EventArgs e)
        {
            int nSubID = HelperURL.GetIntFromUrlString("SubID");
            int nKpID = HelperURL.GetIntFromUrlString("KpID");
            int TypeID = HelperURL.GetIntFromUrlString("Type");

            LoadData(nSubID, nKpID, TypeID);
        }
    }
}
